package com.example.supermarket.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.supermarket.entity.Card;
import com.example.supermarket.mapper.CardMapper;
import com.example.supermarket.service.AdminCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>会员卡模块</p>
 * @author Bosen
 * @date 2021/10/13 0:26
 */
@RestController
public class AdminCardController {

    @Autowired
    AdminCardService service;

    @Autowired
    CardMapper cardMapper;

    /**
     * <p>会员卡信息页面</p>
     */
    @RequestMapping("/admin/card")
    public ModelAndView card(ModelAndView modelAndView) {
        // 获取所有会员卡信息
        List<Card> cards = cardMapper.findAll();
        String cardsJson = JSONArray.toJSONString(cards);
        modelAndView.addObject("cardsJson", cardsJson);

        modelAndView.setViewName("admin/card");
        return modelAndView;
    }

    /**
     * <p>创建一张新的会员卡</p>
     * @return
     * 1 创建成功
     * 0 创建失败
     */
    @RequestMapping("/admin/add/card")
    public int addCard(int flag) {
        // 创建会员卡ID
        String cardId = service.createCardId(flag);
        // 执行sql
        return cardMapper.insert(cardId, flag);
    }

    @RequestMapping("/admin/del/card")
    public int deleteCard(int id) {
        return service.delete(id);
    }

    /**
     * <p>修改卡</P>
     * @Date 2021/10/13 22:03
     */
    @RequestMapping("/admin/update/card")
    public int updateCard(int flag,int id){
        return cardMapper.updateById(id,flag);
    }
}
